﻿using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using XFControls.XFPanels.XFPanelModals;
using XFControls.XFPanels;
using System.Drawing;
using System.Windows.Forms;

namespace XFSenseUI.XFItems
{
    public class SenseMenu : IXFModalReceiveMouse
    {
        #region IXFModal Members

        public XFPanelBase Parent { get; set; }
        public XFModalState State { get; set; }
        public Bitmap Buffer { get; set; }
        public XFItemStyle Style { get; set; }
        public int CurHeight { get; set; }
        public string Text { get; set; }
        public event ModalEventHandler OnSuccessAction;

        public XFPanelList MainPanel { get; set; }

        private int _height;

        public SenseMenu()
        {
            Style = new XFItemStyle()
            {
                PaddingLeft = 25,
                PaddingRight = 25,
                PaddingTop = 10,
                BackgroundColor = Color.White
            };

            int width = Parent == null ? Screen.PrimaryScreen.WorkingArea.Width : Parent.Width;
            MainPanel = new SenseListPanel();
            MainPanel.Width = width - (Style.PaddingLeft + Style.PaddingRight);
        }

        public int GetHeight()
        {
            if (_height > 0)
                return _height;

            var rec = this.GetImageRec("menu");
            _height = rec.Height;

            return _height;
        }

        public void ResetHeight() { }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            var rec = this.GetImageRec("menu");
            int width = Parent == null ? Screen.PrimaryScreen.WorkingArea.Width : Parent.Width;
            int localX = (width / 2) - (rec.Width / 2);

            this.DrawUIAlphaChannel("menu", g, new Rectangle(localX, y, rec.Width, rec.Height));

            using (Bitmap b = XFBitmapFactory.GetBitmap(MainPanel.Width, _height))
            {
                using (Graphics gb = Graphics.FromImage(b))
                {
                    gb.Clear(Color.White);
                    MainPanel.PanelPaint(gb);
                }

                g.DrawImage(b, Style.PaddingLeft, Style.PaddingTop);
            }
        }

        public void SendClick(int x, int y)
        {
            disposeBuffer();
            MainPanel.ReceiveMouse(new Point(x, y), XFPanelMouseEvents.Down);
            Parent.Invalidate();
        }

        private void disposeBuffer()
        {
            lock (this)
            {
                if (Buffer != null)
                    Buffer.Dispose();
                Buffer = null;
            }
        }

        public void ClearClick() { }

        public XFModalClickResult GetClickResult()
        {
            return XFModalClickResult.Cancel;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            lock (this)
            {
                if (Buffer != null)
                    Buffer.Dispose();
            }
            if (Style != null)
                Style.Dispose();
            if (MainPanel != null)
                MainPanel.Dispose();
        }

        #endregion

        #region IXFModalReceiveMouse Members

        public void SendMouseMove(int x, int y)
        {
            disposeBuffer();
            MainPanel.ReceiveMouse(new Point(x, y), XFPanelMouseEvents.Move);
            Parent.Invalidate();
        }

        public void SendMouseUp(int x, int y)
        {
            disposeBuffer();
            MainPanel.ReceiveMouse(new Point(x, y), XFPanelMouseEvents.Up);
            Parent.Invalidate();
        }

        #endregion
    }
}
